package com.example.android

import android.annotation.SuppressLint
import android.app.Service
import android.content.Context
import android.content.Intent
import android.os.Binder
import android.os.IBinder
import android.os.Parcel
import android.util.Log
import android.view.Menu
import android.view.MenuInflater
import androidx.appcompat.view.menu.MenuBuilder
import androidx.room.Room
import com.example.android.daos.MessageViewDao
import com.example.android.databases.MessageViewDatabase
import com.example.android.services.*
import com.example.android.utils.Api
import com.example.android.utils.JsonUtil
import com.example.android.utils.websocket.MyWebSocket
import kotlinx.coroutines.DisposableHandle

/**
 * @ClassName ServicesObject
 * @Author Ryan Kamanri
 * @Date 2022/6/1
 * @Description 全局单例服务类对象, 该对象的生命周期与Application绑定
 **/
@SuppressLint("RestrictedApi")
class ServicesObject(context: Context): Binder(), DisposableHandle {

    private val _Context = context

    private var _Menu: Menu = MenuBuilder(_Context)
    val Menu get() = _Menu

    // Add your services...
    // common services
    private var _Api: Api

    private var _JsonUtil: JsonUtil
    val JsonUtil get() = _JsonUtil

    private val _HaspMapStorage: HashMap<String, String> = hashMapOf()
    val HashMapStorage get() = _HaspMapStorage

    private var _WebSocket: MyWebSocket
    val WebSocket get() = _WebSocket

    // daos
    private var _MessageViewDao: MessageViewDao
    val MessageViewDao get() = _MessageViewDao

    // specified services
    private var _SharedService: SharedService
    val SharedService get() = _SharedService

    private var _AnnounceService: AnnounceService
    val AnnounceService get() = _AnnounceService

    private var _SidebarService: SidebarService
    val SidebarService get() = _SidebarService

    private var _LogInService: LogInService
    val LogInService get() = _LogInService

    private var _UpdatePasswordService: UpdatePasswordService
    val UpdatePasswordService get() = _UpdatePasswordService


    private var _SelectBedService: SelectBedService
    val SelectBedService get() = _SelectBedService



    private var _InfoService:InfoService
    val InfoService get() = _InfoService

    private var _PayService:PayService
    val PayService get() = _PayService

    private var _OnlineChatService: OnlineChatService
    val OnlineChatService get() = _OnlineChatService


    init {
        MenuInflater(_Context).inflate(R.menu.activity_main_drawer, _Menu)

        _Api = Api(_Context.resources.getString(R.string.api_host))

        _JsonUtil = JsonUtil(_Context)

        _WebSocket = MyWebSocket(_Context.resources.getString(R.string.web_socket_host))

        _MessageViewDao = MessageViewDatabase.getDataBase(context).messageViewDao()

        _SharedService = SharedService(_Context, _Api, _JsonUtil, _HaspMapStorage)

        _LogInService = LogInService(_Api, _JsonUtil, _HaspMapStorage, _SharedService)

        _UpdatePasswordService = UpdatePasswordService(_Api, _JsonUtil)

        _AnnounceService = AnnounceService(_Api, _JsonUtil)

        _InfoService = InfoService(_Api, _JsonUtil,_HaspMapStorage)

        _SidebarService = SidebarService(_WebSocket, _Api, _JsonUtil, _HaspMapStorage, _SharedService)

        _SelectBedService = SelectBedService(_Api, _JsonUtil, _HaspMapStorage)

        _PayService = PayService(_Api, _JsonUtil)

        _OnlineChatService = OnlineChatService(_Api, _JsonUtil)
    }

    override fun dispose() {
        Log.d(toString(), "On Distroy the Service")
        _WebSocket.Close()
    }
}